package com.slz3.business.module.goodsManagement.gooGoods.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooGoods.enums.GoodsIsactiveEnum;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class GooGoodsFactory {

    /**
     * 初始化基础属性值
     */
    public static GooGoods initBaseFields(GooGoods gooGoods) {
        if (gooGoods.getUuid() == null) {
            gooGoods.setUuid(UUIDUtils.generateUUID());
            gooGoods.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(gooGoods.getSerAudit())) {
                gooGoods.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(gooGoods.getSerUsing())) {
                gooGoods.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            gooGoods.setLastModifyTime(LocalDateTime.now());
        }
        return gooGoods;
    }

    /**
     * 初始化基础属性值
     */
    public static GooGoods initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static GooGoods newTemStyle() {
        return new GooGoods();
    }

    public static GooGoodsCondition newConditionByReqParams(MultiValueMap<String, String> params) {
        GooGoodsCondition condition = new GooGoodsCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serGoodsIsactive = params.getFirst("serGoodsIsactive");
        List<String> serAuditList = params.get("serAuditList");
        List<String> fkBrandList = params.get("fkBrandList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, GooGoodsCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, GooGoodsCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, GooGoodsCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, GooGoodsCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerGoodsIsactive(VE.isEmpty(serGoodsIsactive) ? null : GoodsIsactiveEnum.valueOf(serGoodsIsactive));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : serAuditList.stream().map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));
        condition.setFkBrandList(VE.isEmpty(fkBrandList) ? null : fkBrandList.stream().map(item -> UUID.fromString(item)).collect(Collectors.toList()));

        return condition;
    }

    public static GooGoods newBySerAmountCurrentTotal(GooGoods domain, Long serAmountCurrentTotal) {
        if (serAmountCurrentTotal == null) {
            return null;
        }
        GooGoods goods = new GooGoods();
        Steward.merge(domain, goods);
        goods.setSerGoodsStock(serAmountCurrentTotal.intValue());
        GooGoodsFactory.initBaseFields(goods);
        return goods;
    }
}
